"""

@name:python学习
@author:yuki
@projectname:起步

"""

import os
import IPy
import subprocess
import socket
import platform
import threading


def find_local_ip(): ####查看当前ip
    myname = socket.getfqdn(socket.gethostname())
    myaddr = socket.gethostbyname(myname)
    print(myaddr)
def ping_ip(ip_str):
    print(ip_str)
    cmd = ["ping", "-n", "1", f"{ip_str}"]
    output = os.popen(" ".join(cmd)).readlines()
    for line in output:
        if str(line).upper().find("TTL") >= 0:
            print(f'False,the ip:{ip} is using now')
            with open("ip_analysis.txt", "a+") as fp:
                fp.write(f'False,the ip:{ip} is using now\n')
            break
        else:
            print(f'False,the ip:{ip} is no one using now')
            with open("ip_analysis.txt", "a+") as fp:
                fp.write(f'False,the ip:{ip} is no one using now\n')
            break

# def get_ping_result(ip):  # ping ip 地址 ping通则返回
#     cmd_str = "ping {0} -n 1 -w 0.1".format(ip)
#     DETACHED_PROCESS = 0x00000008  # 不创建cmd窗口
#     try:
#         a = subprocess.run(cmd_str, creationflags=DETACHED_PROCESS, check=True)  # 仅用于windows系统
#         print(a)
#     except subprocess.CalledProcessError as err:
#         # print(f'False,the ip:{ip} is using now')
#         with open("ip_analysis.txt","a+") as fp:
#             fp.write(f'False,the ip:{ip} is using now\n')
#     else:
#         return ip

find_local_ip()
fp = open("ip_analysis.txt","w")
ips = IPy.IP(input("please input your ip and network:"))
for ip in ips:
    ping_ip(ip)
